﻿using JsonArchive;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HousekeepingSystem
{
    public partial class Form2_1 : Form
    {
        public Form2_1()
        {
            InitializeComponent();
        }

        private void Form2_1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "家政人员")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("保洁");
            }
            if (comboBox1.SelectedItem.ToString() == "内勤人员")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("介绍");
                comboBox2.Items.Add("班长");
                comboBox2.Items.Add("主管");
            }
            comboBox2.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkEmployeeList()) return;
            saveEmployeeList();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private bool checkEmployeeList()
        {
            if (textBox1.Text.Trim() == "")
            {
                textBox1.Focus();
                MessageBox.Show("员工姓名不能为空","错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox2.Text);
                if (textBox2.Text.Length != 11)
                {
                    textBox2.Focus();
                    MessageBox.Show("联系电话长度错误", "请使用手机号码");
                    return true;
                }
            }
            catch
            {
                textBox2.Focus();
                MessageBox.Show("联系电话格式错误", "错误");
                return true;
            }
            return false;
        }

        //保存数据
        private void saveEmployeeList()
        {
            String JSON = File.ReadAllText(Application.StartupPath + @"\info\EmployeeList.json");
            var EmployeeList = J2D.JsonToDictionary(JSON);

            ArrayList ContractRowValue = new ArrayList();
            ContractRowValue.Add(textBox1.Text);
            ContractRowValue.Add(comboBox1.SelectedItem.ToString());
            ContractRowValue.Add(comboBox2.SelectedItem.ToString());
            ContractRowValue.Add(textBox2.Text);
            int lastTable = Convert.ToInt32(EmployeeList.Keys.Last());
            EmployeeList.Add((lastTable + 1).ToString(), ContractRowValue.ToArray());

            JSON = D2J.DictionaryToJson(EmployeeList);
            File.WriteAllText(Application.StartupPath + @"\info\EmployeeList.json", JSON);
        }

        private void Form2_1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.button1_Click(sender, e);
                e.Handled = true;
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.button2_Click(sender, e);
                e.Handled = true;
            }
        }
    }
}
